package com.test.slice;

import com.ms.banner.Banner;
import com.ms.banner.util.Utils;
import com.ms.banner.listener.OnBannerClickListener;
import com.test.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;

import static com.test.MyApplication.getDataInt;

public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initListContainer();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initListContainer() {
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        List<SampleItem> list = getData();
        SampleItemProvider sampleItemProvider = new SampleItemProvider(list, this);
        listContainer.setItemProvider(sampleItemProvider);
        listContainer.setItemClickedListener((container, component, position, id) -> {
            switch (position){
                case 0:
                    present(new BannerAnimationAbilitySlice(),new Intent());
                    break;
                case 1:
                    Intent intent1 = new Intent();
                    // 指定待启动FA的bundleName和abilityName
                    // 指定设备标识（空串表示当前设备）、应用包名、Ability名称
                    Operation operation1 = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName(getBundleName())
                            .withAbilityName("com.test.BannerStyleAbility")
                            .build();
                    intent1.setOperation(operation1);
                    // 通过AbilitySlice的startAbility接口实现启动另一个页面
                    startAbility(intent1);
                    break;
                case 2:
                    Intent intent2 = new Intent();
                    // 指定待启动FA的bundleName和abilityName
                    // 指定设备标识（空串表示当前设备）、应用包名、Ability名称
                    Operation operation2 = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName(getBundleName())
                            .withAbilityName("com.test.IndicatorPositionAbility")
                            .build();
                    intent2.setOperation(operation2);
                    // 通过AbilitySlice的startAbility接口实现启动另一个页面
                    startAbility(intent2);
                    break;
                case 3:
                    Intent intent3 = new Intent();
                    // 指定待启动FA的bundleName和abilityName
                    // 指定设备标识（空串表示当前设备）、应用包名、Ability名称
                    Operation operation3 = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName(getBundleName())
                            .withAbilityName("com.test.CustomBannerAbility")
                            .build();
                    intent3.setOperation(operation3);
                    // 通过AbilitySlice的startAbility接口实现启动另一个页面
                    startAbility(intent3);
                    break;
                case 4:
                    Intent intent4 = new Intent();
                    // 指定待启动FA的bundleName和abilityName
                    // 指定设备标识（空串表示当前设备）、应用包名、Ability名称
                    Operation operation4 = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName(getBundleName())
                            .withAbilityName("com.test.BannerLocalAbility")
                            .build();
                    intent4.setOperation(operation4);
                    // 通过AbilitySlice的startAbility接口实现启动另一个页面
                    startAbility(intent4);
//                    present();
                    break;
                case 5:
                    Intent intent5 = new Intent();
                    // 指定待启动FA的bundleName和abilityName
                    // 指定设备标识（空串表示当前设备）、应用包名、Ability名称
                    Operation operation5 = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName(getBundleName())
                            .withAbilityName("com.test.CustomViewPagerAbility")
                            .build();
                    intent5.setOperation(operation5);
                    // 通过AbilitySlice的startAbility接口实现启动另一个页面
                    startAbility(intent5);
                    break;
                case 6:
                    present(new SimpleFragmentAbilitySlice(),new Intent());
                    break;
            }
        });

        Banner banner = (Banner) findComponentById(ResourceTable.Id_banner);
        if(banner == null){
            return;
        }
        banner.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, (int) (Utils.getInstance().getScreenPiex(this).height/4)));
        //简单使用
        banner.setAutoPlay(true)
                .setPages(getDataInt(), new CustomViewHolder())
                .setDelayTime(3000)
                .setOnBannerClickListener(new OnBannerClickListener() {
                    @Override
                    public void onBannerClick(List datas, int position) {
                        new ToastDialog(banner.getContext())
                                .setText("you clicked:"+position)
                                // Toast显示在界面中间
                                .setAlignment(LayoutAlignment.CENTER)
                                .show();
                    }
                })
                .start();

    }
    private ArrayList<SampleItem> getData() {
        ArrayList<SampleItem> list = new ArrayList<>();
            list.add(new SampleItem("banner动画预览"));
            list.add(new SampleItem("banner指示器样式预览"));
            list.add(new SampleItem("banner指示器位置设置预览"));
            list.add(new SampleItem("banner一些自定义样式方法预览"));
            list.add(new SampleItem("banner加载本地图片"));
            list.add(new SampleItem("banner自定义布局文件"));
            list.add(new SampleItem("ViewPager + Fragment + Banner"));
            list.add(new SampleItem("banner更多用法请看文档，这里就不一一列举了！"));
        return list;
    }
    private ArrayList<String> getDataStr() {
        ArrayList<String> list = new ArrayList<>();
            list.add("https://upload-images.jianshu.io/upload_images/9913211-1c553827bdae32d2.jpg");
            list.add("https://upload-images.jianshu.io/upload_images/9913211-a6e6062a81d9258d.jpg");
        return list;
    }



}
